<?php /** @var commodityRequire $thatRequire */ ?>
<?php if (isset($thatRequire)): ?>
<div id="edit-require-dialog">
    <form id="edit-require-form">
        <input type="hidden" name="commodityrequire_id" value="<?php echo $thatRequire->getId(); ?>"/>
        <input type="hidden" name="user_id" value="<?php echo $thatRequire->getUserId(); ?>"/>

        <div class="left-right-side">
            <div class="input-group left-side">
                <label for="commodity-require-title" class="input-group-addon">求购标题</label>
                <input type="text" name="commodity_require_title" id="commodity-require-title" class="form-control" maxlength="30" required="required" placeholder="请填写求购标题" value="<?php echo $thatRequire->getTitle() ?>""/>
            </div>
            <div class="input-group right-side">
                <label for="release-type" class="input-group-addon">求购类型</label>
                <select id="release-type" name="release-type" class="form-control">
                    <option value="1" <?php if($thatRequire->getReleaseType()==1)echo 'selected'; ?> >求物</option>
                    <option value="3" <?php if($thatRequire->getReleaseType()==3)echo 'selected'; ?>>求帮</option>
                </select>
            </div>

        </div>
        <div class="left-right-side">
            <div class="input-group left-side">
                <label for="commodity-require-min-price" class="input-group-addon">价格下限</label>
                <input type="number" name="commodity_require_min_price" id="commodity-require-min-price" class="form-control" placeholder="请填写价格下限"  value="<?php echo $thatRequire->getMinPrice() ?>">
            </div>
            <div class="input-group right-side">
                <label for="commodity-require-max-price" class="input-group-addon">价格上限</label>
                <input type="number" name="commodity_require_max_price" id="commodity-require-max-price" class="form-control" placeholder="请填写价格上限"  value="<?php echo $thatRequire->getMaxPrice() ?>">
            </div>
        </div>
        <div class="left-right-side">
            <div class="input-group left-side">
                <label for="commodity-contact-info" class="input-group-addon">联系方式</label>
                <select id="commodity-contact-type" name="commodity_contact_type" class="form-control" style="float:left; width:32%">
                    <option value="<?php echo _ContactType_LongPhone ?>" <?php if($thatRequire->getContactType()==_ContactType_LongPhone) echo "selected"?> >长号</option>
                    <option value="<?php echo _ContactType_ShortPhone ?>" <?php if($thatRequire->getContactType()==_ContactType_ShortPhone) echo "selected"?>>短号</option>
                    <option value="<?php echo _ContactType_Qq ?>" <?php if($thatRequire->getContactType()==_ContactType_Qq) echo "selected"?>>QQ</option>
                </select>
                <input type="text" id="commodity-contact-info" name="commodity_contact_info" class="form-control" style="float:left; width:68%" value="<?php echo $thatRequire->getContactInfo() ?>"/>
            </div>
            <div class="input-group right-side">
                <label for="commodity-require-num" id="commodity-require-num-span" class="input-group-addon">需求数量</label>
                <input type="number" id="commodity-require-num" name="commodity_require_num" class="form-control" min="0" max="10000" placeholder="请输入库存"  value="<?php echo $thatRequire->getNum() ?>"/>
            </div>
        </div>
        <div class="input-group-has-textarea">
            <label for="commodity-require-content" class="input-group-addon">求购描述</label>
            <textarea id="commodity-require-content" name="commodity_require_content" class="form-control" rows="4" placeholder="请填写描述"> <?php echo $thatRequire->getContent() ?></textarea>
        </div>
        <div class="input-group">
            <input type="submit" class="form-control" value="发布！"/>
        </div>
    </form>
</div>
<?php endif; ?>
<script>
    $(function () {
        $("#release-type").change(function () {
            if ($("#release-type").val() == "3") {
                $('#commodity-require-min-price').val("0");
                $('#commodity-require-min-price').attr("readonly", "readonly");
                $('#commodity-require-max-price').val("0");
                $('#commodity-require-max-price').attr("readonly", "readonly");
                $('#commodity-require-num-span').html("答谢报酬");
                $('#commodity-require-num').attr('placeholder',"请写入答谢的方式");
            } else {
                $('#commodity-require-min-price').removeAttr("readonly");
                $('#commodity-require-min-price').val("");
                $('#commodity-require-max-price').removeAttr("readonly");
                $('#commodity-require-max-price').val("");
                $('#commodity-require-num-span').html("需求数量");
            }
        });

        $("#edit-require-form").validate({
            submitHandler: function (form) {
                $.ajax({
                    url: '<?php echo base_url('api/editRequire');?>',
                    type: 'post',
                    data: $(form).serialize(),
                    beforeSend: function () {
                        showProgressNoty();
                    },
                    complete: function () {
                        closeProgressNoty();
                    },
                    success: function (jsonrsp) {
                        try {
                            var rsp = $.parseJSON(jsonrsp);
                            noty({type: rsp.type, text: rsp.message, callback: {
                                afterClose: function () {
                                    switch (rsp.type) {
                                        case 'success':
                                            location.reload();
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }});
                        } catch (ex) {
                            noty({type: 'error', text: '发生内部错误，请联系管理员'});
                            throw ex;
                        }
                    }
                });
            }
        });

        var $commodityCategory = $('#commodity-category');
        $commodityCategory.val($commodityCategory.children().first().val());
        var $subCatSelect = $('#commodity-sub-category');

        function showSubCatByFatherId(fatherId) {
            var $nowToShowSubCat = $('option[data-father-id=' + fatherId + ']');
            $subCatSelect.children().hide();
            $nowToShowSubCat.show();
            $subCatSelect.val($nowToShowSubCat.first().val());
        }

        showSubCatByFatherId($commodityCategory.children().first().val());

        $commodityCategory.click(function () {
            var nowMasterCatId = $(this).val();
            $subCatSelect.children().hide();
            var $nowToShowSubCat = $('option[data-father-id=' + nowMasterCatId + ']');
            if ($nowToShowSubCat.length) {
                $nowToShowSubCat.show();
                $subCatSelect.show();
                $commodityCategory.css('width', '32%');
            } else {
                $subCatSelect.hide();
                $commodityCategory.css('width', '100%');
            }
            $subCatSelect.val($nowToShowSubCat.first().val());
        });
    });
</script>